package gui.properties.groups;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import model.Model;

import util.reflection.MethodAction;
import util.reflection.Reflection;
import controller.AbstractController;
import controller.PropertiesController;
import controller.SLogoController;
/**
 * @author Shun Fan
 * 
 */
public class SliderGroup extends AbstractGroup{
	private String receiveMethodName;
	private PropertiesController myController;
	private JSlider myTrailWidthSlider;
	private static final String SCANNER_DELIMITTER = ";\\s*";
	
	public SliderGroup(Model model, PropertiesController controller, String parameterLine) {
		myController = controller;
		
		System.out.println("SliderGroup start");
		Scanner reader = new Scanner(parameterLine).useDelimiter(SCANNER_DELIMITTER);
		String labelName = reader.next();
		String methodName = reader.next();
		receiveMethodName = reader.next();
		
		String slider_min = reader.next();
		String slider_max = reader.next();
		
		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();

		labelTable.put( Integer.parseInt(slider_min), new JLabel( slider_min) );
		labelTable.put( Integer.parseInt(slider_max), new JLabel( slider_max) );
		
		myTrailWidthSlider = new JSlider(JSlider.HORIZONTAL, Integer.parseInt(slider_min), 
				Integer.parseInt(slider_max), Integer.parseInt(slider_max)/2);
		myTrailWidthSlider.setLabelTable(labelTable);
		myTrailWidthSlider.setPaintLabels(true);
		myTrailWidthSlider.setMajorTickSpacing(1);
        myTrailWidthSlider.setPaintTicks(true);
        myTrailWidthSlider.setSnapToTicks(true);
        
		myTrailWidthSlider.addChangeListener(
				new MethodAction(controller, methodName, model, myTrailWidthSlider)		
			);
		
		add(new JLabel(labelName));
		add(myTrailWidthSlider);
	}
	
	public void update() {
		Reflection.callMethod(myController, receiveMethodName, myTrailWidthSlider);	
	}
	
	public static void main(String args[]) throws IOException {
		Model model = new Model();
		SLogoController controller = new SLogoController(model);
		PropertiesController prop = new PropertiesController(model);
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//f.setLayout(new FlowLayout());
		
		
		SliderGroup c = new SliderGroup(model, prop, "; Turtle Speed; setTrailWidth; 0; 10;");
		f.add(c);
		f.setSize(1000,500);
		f.setVisible(true);
	}


}
